package com.itheima.controller;

import com.itheima.domain.Category;
import com.itheima.dto.CategoryPostDto;
import com.itheima.dto.CategoryPutDto;
import com.itheima.service.CategoryService;
import com.itheima.vo.CategoryPageVo;
import com.itheima.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * date: 2022/5/12 21:09
 *
 * @author daya000
 */
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService service;

    @PostMapping("")
    public Result addCategory(@RequestBody CategoryPostDto dto) {
        //1.判断参数是否为空
        if (Objects.isNull(dto)) {
            return new Result(0, "参数不能为空", null, null);
        }
        //2.判断新增分类是否存在数据库
        Category nameExist = service.selectByname(dto.getName());
        if (nameExist != null) {
            return new Result(0, "分类名已存在", null, null);
        } else {
            Category category = new Category();
            BeanUtils.copyProperties(dto, category);

            Boolean result = service.addCategory(category);

            Integer code = result ? 1 : 0;
            String msg = result ? "添加成功" : "分类名已存在，添加失败";
            return new Result(code, msg, null, null);
        }
    }

    @PutMapping("")
    public Result updateCategory(@RequestBody CategoryPutDto dto) {
        //判断参数是否为空
        if (Objects.isNull(dto)) {
            return new Result(0, "参数不能为空", null, null);
        }
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        service.updateCategory(category);

        return new Result(1, null, "修改分类信息成功", null);
    }

    @DeleteMapping
    public Result deleteCategory(@RequestParam("id") String id) {
        //判断参数是否为空
        if (id == null) {
            return new Result(0, "id不能为空", null, null);
        }

        Boolean result = service.deleteById(id);

        int code = result ? 1 : 0;
        String msg = result ? "删除成功" : "当前分类下关联了套餐，不能删除";
        return new Result(code, msg, null, null);
    }

    @GetMapping("/page")
    public Result selectByPage(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        if (page <= 0) {
            return new Result(0, "页面错误", null, null);
        }

        //获取分页数据
        List<Category> categoryPage = service.selectByPage(page, pageSize);
        //获取总条数
        Long count = service.selectTotal(name);
        CategoryPageVo vo = new CategoryPageVo();

        vo.setRecords(categoryPage);
        vo.setTotal(count);
        vo.setSize(pageSize);
        vo.setCurrent(page);
        return new Result(1,null,vo,null);
    }

    @GetMapping("/list")
    public Result selectAll(@RequestParam("type") String type){
        List<Category> categoryList = service.selectList();
        Result result = new Result();
        result.setData(categoryList);
        return new Result(1,null,categoryList,null);

    }
}
